﻿using Butcher.winform.Dto;
using Newtonsoft.Json.Linq;
using System.Text.Json;
using slaughters.Dto;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics.Metrics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Butcher.winform.EnterpriseForms;
using Traceability.Commont.Domains;

namespace slaughters
{
    public partial class Department : Form
    {
        public Department()
        {
            InitializeComponent();
        }

        private void button_Click(object sender, EventArgs e)
        {
            var level = cb_Level.SelectedValue;
            var name = tb_Name.Text;
            var type = 0;
            if (cb_type.Text == "生产部")
            {
                type = 1;
            }
            else
            {
                type = 2;
            }

            var depa = new Departments()
            {
                Levelid = Convert.ToInt32(level.ToString()),
                DepartmentName = name,
                Typeid = type,
            };
            string url = @"http://localhost:5229/api/Section/SectionCreate";
            HttpClient client = new HttpClient();
            var content = new StringContent(JsonSerializer.Serialize<Departments>(depa));
            content.Headers.Remove("Content-Type");
            content.Headers.Add("Content-Type", "application/json");
            string result = client.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;
           // Departmentview list = new Departmentview();
            JObject info = JObject.Parse(result);
            if (result != null)
            {
                MessageBox.Show("新增成功");
            }
            //this.Hide();
            //list.ShowDialog();
            
            if (result != null)
            {
                MessageBox.Show("新增成功");
            }
            else
            {
                MessageBox.Show("新增失败");
            }

        }

        private void Department_Load(object sender, EventArgs e)
        {
            GetEnter infoEnter = new GetEnter();
            infoEnter.EnterpriseName = "";
            infoEnter.pageIndex = 1;
            infoEnter.pageSize = 10;
            string url = @"http://localhost:5229/api/Enterprise/GetEnterprises";
            HttpClient client = new HttpClient();
            var content = new StringContent(JsonSerializer.Serialize<GetEnter>(infoEnter));
            content.Headers.Remove("Content-Type");
            content.Headers.Add("Content-Type", "application/json");
            string result = client.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;
            JObject info = JObject.Parse(result);
            var saaa = result;
            var aaa = info["lists"].Value<JArray>();
            List<Enterprises> enterprises = aaa.ToObject<List<Enterprises>>();
            this.cb_Level.DataSource = enterprises;
            this.cb_Level.DisplayMember = "EnterpriseName";
            this.cb_Level.ValueMember = "Enterpriseid";

        }
    }
}
